package com.boco.pciv.core.controller;

import com.boco.pciv.core.entity.Constants;
import com.boco.pciv.core.security.LoginUser;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;

/**
 * Created by 操 on 2017/01/14.
 *
 */
public class BaseController {
    private  static Logger log = LoggerFactory.getLogger(BaseController.class);
    protected static String SUCCESS = "SUCCESS";
    protected static String ERROR = "ERROR";

    protected static String REDIRECT = "redirect:";
    protected static String FORWARD = "forward:";

    protected static String MSG_ExeERROR = "执行出错";
    protected static String MSG_ExeOK = "执行成功";

    @Autowired
    private HttpServletRequest request;

    /**
     * 获取当前用户信息
     * @return 当前登录用户
     */
    protected LoginUser getCurUser(){
        Object obj = request.getSession().getAttribute(Constants.CurrentUser);
        if (obj==null)
            return null;
        else  return (LoginUser)obj;
    }
    /**
     * 获取当前用户ID
     * @return 登录用户id
     */
    protected String getCurUserId(){
        Object obj = request.getSession().getAttribute(Constants.CurrentUserId);
        if (obj==null || StrUtil.isBlank(obj.toString())) {
            LoginUser user = getCurUser();
            if (user == null)
                return "";
            else return user.getUserId();
        }
        else {
            return obj.toString();
        }
    }

    /**
     * 检查当前用户是否登录
     * @return true:已登录 false:未登录
     */
    protected Boolean checkUserLogin(){
        return getCurUser() != null;
    }

    /**
     * 获取session id信息
     * @return session id
     */
    protected String getSessionId(){
        return request.getSession().getId();
    }
    /**
     * 获取session数据
     * @param key sessionkey
     * @return session值
     */
    protected Object getSessionData(String key){
        return request.getSession().getAttribute(key);
    }

    /**
     * 设置session数据
     * @param key
     * @param value
     */
    protected void setSessionData(String key, Serializable value){
        request.getSession().setAttribute(key,value);
    }

    /**
     * @return 当前用户
     */
    protected String getUserIp(){
        return request.getRemoteAddr();
    }

    /**
     * 获取当前模块id
     * @return 模块Id
     */
    protected String getCurModelId(){
        String resid = request.getParameter(Constants.CurResId);
        if(StrUtil.isBlank(resid)){
            resid = request.getHeader(Constants.CurResId);
        }
        return resid;
    }

    /**
     * 返回前台文件流
     */
    protected ResponseEntity<byte[]> renderFile(String fileName) {
        byte[] bytes = FileUtil.readBytes(new File(fileName));
        return renderFile(fileName, bytes);
    }

    /**
     * 返回前台文件流

     */
    protected ResponseEntity<byte[]> renderFile(String fileName, byte[] fileBytes) {
        String dfileName = null;
        try {
            dfileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
        } catch (UnsupportedEncodingException e) {
            log.warn("文件读取失败",e);
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", dfileName);
        return new ResponseEntity<byte[]>(fileBytes, headers, HttpStatus.CREATED);
    }
}
